package texture
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	/**
	 * 位图扩展 
	 * @author weiping
	 * 
	 */
	public class BitmapEx extends Bitmap
	{
		
		private var backupData:BitmapData;	//图像备份
		
		protected var scale9GridRect : Rectangle = null; 
		
		public function BitmapEx(data:BitmapData,scale9Grid:Rectangle = null)
		{
			backupData = data;
			
			scale9GridRect = scale9Grid;
			
			if(scale9GridRect)
			{
				updataBitmapData(backupData);
			}
			
			super(data);
		}
		
		/**
		 * 更新图像 
		 * @param value
		 * 
		 */
		public function updataBitmapData(value:BitmapData):void
		{
			//更新备份图像
			backupData = value;

			if (scale9GridRect != null && backupData) 
			{
				if (!validGrid(scale9GridRect)) 
				{
					scale9GridRect = null;
				}
				SetSize(backupData.width, backupData.height);
			} 
			else 
			{
				//更新当前的图像
				assignBitmapData(backupData);
			}
		}
		
		private function validGrid(r : Rectangle) : Boolean 
		{
			return r.right <= backupData.width && r.bottom <= backupData.height;
		}
		
		public function SetSize(w : Number, h : Number) : void 
		{
			if (scale9GridRect == null) 
			{
				super.width = w;
				super.height = h;
			} 
			else 
			{
				var wv:Number = Math.max(w, backupData.width - scale9GridRect.width);
				var hv:Number = Math.max(h, backupData.height - scale9GridRect.height);
				ResizeBitmap(wv, hv);
			}
		}
		
		protected function ResizeBitmap(w : Number, h : Number) : void 
		{
			
			var bmpData : BitmapData = new BitmapData(w, h, true, 0x00000000);

			var nX:int;
			var nY:int;
			var nW:int;
			var nH:int;
			
			var i:int;
			var n:int;
			var d:int;
			
			var nSizeW:int = w- (backupData.width - scale9GridRect.width);
			var nSizeH:int = h- (backupData.height - scale9GridRect.height);
			//上左
			bmpData.copyPixels(backupData,new Rectangle(0,0,scale9GridRect.left,scale9GridRect.top),new Point(0,0));
			
			//上中
			n = nSizeW / scale9GridRect.width;
			d = nSizeW % scale9GridRect.width;
			nX = scale9GridRect.x;
			nY = 0;
			nW = scale9GridRect.width;
			nH = scale9GridRect.y;
			
			for(i=0; i<n; i++)
			{
				bmpData.copyPixels(backupData,new Rectangle(nX,nY,nW,nH),new Point(scale9GridRect.x+i*nW,0));
			}
			bmpData.copyPixels(backupData, new Rectangle(nX, nY, d, nH), new Point(scale9GridRect.x+i*nW, 0));
			
			//上右
			nX = scale9GridRect.right;
			nY = 0;
			nW = backupData.width - scale9GridRect.right;
			nH = scale9GridRect.y;
			bmpData.copyPixels(backupData,new Rectangle(nX,nY,nW,nH),new Point(scale9GridRect.x + nSizeW,0));
			
			
			//中左 
			n = nSizeH / scale9GridRect.height;
			d = nSizeH % scale9GridRect.height;
			nX = 0;
			nY = scale9GridRect.y;
			nW = scale9GridRect.left;
			nH = scale9GridRect.height;
			
			for(i=0; i<n; i++)
			{
				bmpData.copyPixels(backupData,new Rectangle(nX,nY,nW,nH),new Point(0,scale9GridRect.y+i*nH));
			}
			bmpData.copyPixels(backupData, new Rectangle(nX, nY, nW, d), new Point(0, scale9GridRect.y+i*nH));
			
			//中
			nX = scale9GridRect.x;
			nY = scale9GridRect.y;
			nW = scale9GridRect.width;
			nH = scale9GridRect.height;
			n = nSizeW;
			d = nSizeH;
			
			var bit:BitmapData = new BitmapData(nW,nH);
			bit.copyPixels(backupData,new Rectangle(nX,nY,nW,nH),new Point(0,0));
			var sprite:Sprite = new Sprite();
			sprite.graphics.beginBitmapFill(bit);
			sprite.graphics.drawRect(0,0,n,d);
			sprite.graphics.endFill();
			var data:BitmapData = new BitmapData(sprite.width,sprite.height);
			data.draw(sprite);
			bmpData.copyPixels(data, new Rectangle(0, 0, n, d), new Point(nX, nY));
			bit.dispose();
			data.dispose();
			sprite = null;
			
			//中右
			n = nSizeH / scale9GridRect.height;
			d = nSizeH % scale9GridRect.height;
			nX = scale9GridRect.right;
			nY = scale9GridRect.y;
			nW = backupData.width - scale9GridRect.right;
			nH = scale9GridRect.height;
			for(i=0; i<n; i++)
			{
				bmpData.copyPixels(backupData,new Rectangle(nX,nY,nW,nH),new Point(scale9GridRect.x + nSizeW,scale9GridRect.top+i*nH));
			}
			bmpData.copyPixels(backupData, new Rectangle(nX, nY, nW, d), new Point(scale9GridRect.x + nSizeW, scale9GridRect.top+i*nH));
			
			//下左
			nX = 0;
			nY = scale9GridRect.bottom;
			nW = backupData.width - scale9GridRect.right;
			nH = backupData.height - scale9GridRect.bottom;
			bmpData.copyPixels(backupData,new Rectangle(nX,nY,nW,nH),new Point(0,scale9GridRect.y + nSizeH));
			//下中
			n = nSizeH / scale9GridRect.width;
			d = nSizeH % scale9GridRect.width;
			nX = scale9GridRect.left;
			nY = scale9GridRect.bottom;
			nW = scale9GridRect.width;
			nH = backupData.height - scale9GridRect.bottom;
			for(i=0; i<n; i++)
			{
				bmpData.copyPixels(backupData,new Rectangle(nX,nY,nW,nH),new Point(scale9GridRect.x+i*nW, scale9GridRect.y + nSizeH));
			}
			bmpData.copyPixels(backupData, new Rectangle(nX, nY, d, nH), new Point(scale9GridRect.x+i*nW, scale9GridRect.y + nSizeH));
			
			//下右
			nX = scale9GridRect.right;
			nY = scale9GridRect.bottom;
			nW = backupData.width - scale9GridRect.right;
			nH = backupData.height - scale9GridRect.bottom;
			bmpData.copyPixels(backupData,new Rectangle(nX,nY,nW,nH),new Point(scale9GridRect.x + nSizeW,scale9GridRect.y + nSizeH));
			
			assignBitmapData(bmpData);
		}
		
		override public function set width(w : Number) : void 
		{
			if (w != width) 
			{
				SetSize(w, height);
			}
		}
		

		override public function set height(h : Number) : void 
		{
			if (h != height) 
			{
				SetSize(width, h);
			}
		}
		
		override public function set scale9Grid(r : Rectangle) : void 
		{
			if ((scale9GridRect == null && r != null) || (scale9GridRect != null && !scale9GridRect.equals(r))) 
			{
				if (r == null) 
				{
					var currentWidth : Number = width;
					var currentHeight : Number = height;
					scale9GridRect = null;
					assignBitmapData(backupData);
					SetSize(currentWidth, currentHeight);
				} 
				else 
				{
					if (!validGrid(r)) 
					{
						throw (new Error("#001 - The _scale9Grid does not match the original BitmapData"));
						return;
					}
					
					scale9GridRect = r.clone();
					ResizeBitmap(width, height);
					scaleX = 1;
					scaleY = 1;
				}
			}
		}
		
		/**
		 * 剪切位图 
		 * @param rect  	//剪切大小
		 * @param alphaBitmapData	 //庶照BitmapData
		 * @param alphaPoint				//庶照图像的起点坐标
		 * @param mergeAlpha			//是否起动alpha
		 * 
		 */
		public function clipBitmapData(rect:Rectangle, alphaBitmapData:BitmapData = null, alphaPoint:Point = null, mergeAlpha:Boolean = true):void
		{
			if(!backupData)
				return;
			
			var bitmapData:BitmapData = new BitmapData(rect.width, rect.height, true, 0x00000000);
			var pos:Point = new Point(0, 0);
			bitmapData.copyPixels(backupData, rect, pos, alphaBitmapData, alphaPoint, mergeAlpha);
			assignBitmapData(bitmapData);
		}
		
		/**
		 * 重置当前位图 
		 * @param bmp
		 * 
		 */		
		private function assignBitmapData(bmp : BitmapData) : void 
		{
			if(super.bitmapData && (super.bitmapData != backupData)) super.bitmapData.dispose();
			super.bitmapData = bmp;
		}
		
		public function get backupBitmapData():BitmapData
		{
			return backupData;
		}
		
		/**
		 * 获取按扭位图列表 
		 * @return 
		 * 
		 */		
		public static function getButtonImgList(bitmapdata:BitmapData):Vector.<BitmapEx>
		{
			if(!bitmapdata)
				return null;
			
			var imglist:Vector.<BitmapEx> = new Vector.<BitmapEx>;
			
			var w:int = bitmapdata.width / 3;
			var h:int = bitmapdata.height;
			
			for(var i:int=0; i<3; i++)
			{
				var data:BitmapData = new BitmapData(w, h, true, 0x00000000);
				var rect:Rectangle = new Rectangle(i*w,0, w, h);
				var pos:Point = new Point(0, 0);
				data.copyPixels(bitmapdata, rect, pos,null,null,true);
				var bit:BitmapEx = new BitmapEx(data);
				imglist.push(bit);
			}
			return imglist;
		}
		
		public function dispose():void
		{
			if(backupData)
				backupData.dispose();
			
			if(bitmapData)
				bitmapData.dispose();
		}
	}
}